package com.yqya.springboot.hello.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.yqya.springboot.hello.service.HelloService;

/**
 * hello service 自定义start自动化配置
 *
 * @author Kevin
 * @date 2019-07-26
 */

@Configuration // 开启配置
@EnableConfigurationProperties(value = HelloServiceProperties.class) // 开启使用映射实体对象
@ConditionalOnClass(HelloService.class) // 存在HelloService时初始化该配置类
@ConditionalOnProperty(
        prefix = "hello", // 配置前缀
        value = "enable", // 开启
        matchIfMissing = true // 缺失检查
)
public class HelloServiceAutoConfiguration {

    /**
     * application.properties配置文件映射前缀实体对象
     */
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    /**
     * 根据条件判断不存在HelloService时，初始化新bean到Spring IoC
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        helloService.setShow(helloServiceProperties.isShow());
        return helloService;
    }

}
